#include "encoderSetWin.h"

/////5号//////////

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;

/**数据的输入框**/
static lv_obj_t * spinbox_1;
static lv_obj_t * spinbox_2;

/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}

/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
    motor_set_encoderResolution(lv_spinbox_get_value(spinbox_1));
    motor_set_reductionRatio(lv_spinbox_get_value(spinbox_2));
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(5);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(5);
}

/**切换数字输入框的编辑状态 回调函数**/
static void spinbox_cb(lv_event_t * event)
{
    /**判断是否是编辑状态**/
	if(lv_group_get_editing(keyGroup))
	{
		//printf("LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, false);
	}
	else
	{
		//printf("!LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, true);
		//printf("set LV_STATE_FOCUSED\r\n");
	}
}


void open_encoderSetWin()
{
    lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

    lv_obj_t* label1 = lv_label_create(lv_scr_act());
    lv_label_set_text(label1,"Resolution:");
    lv_obj_align(label1,LV_ALIGN_TOP_LEFT,2,8);

    label1 = lv_label_create(lv_scr_act());
    lv_label_set_text(label1,"Gear Ratio:");
    lv_obj_align(label1,LV_ALIGN_TOP_LEFT,2,30);


    spinbox_1 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_1,LV_ALIGN_TOP_LEFT,85,8);
    lv_obj_set_width(spinbox_1,55);
    lv_obj_set_height(spinbox_1,17);
    lv_obj_set_style_pad_bottom(spinbox_1,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_1,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_1, 0, 5000);
    lv_spinbox_set_digit_format(spinbox_1, 4, 0);
	lv_spinbox_set_step(spinbox_1,1);
    lv_spinbox_set_value(spinbox_1,motor_get_encoderResolution());
    lv_obj_add_event_cb(spinbox_1,spinbox_cb,LV_EVENT_PRESSED,NULL);

    spinbox_2 = lv_spinbox_create(lv_scr_act());
    lv_obj_align(spinbox_2,LV_ALIGN_TOP_LEFT,85,30);
    lv_obj_set_width(spinbox_2,55);
    lv_obj_set_height(spinbox_2,17);
    lv_obj_set_style_pad_bottom(spinbox_2,0,LV_PART_MAIN);
    lv_obj_set_style_pad_top(spinbox_2,-1,LV_PART_MAIN);
    lv_spinbox_set_range(spinbox_2, 0, 999);
    lv_spinbox_set_digit_format(spinbox_2, 3, 0);
	lv_spinbox_set_step(spinbox_2,1);
    lv_spinbox_set_value(spinbox_2,motor_get_reductionRatio());
    lv_obj_add_event_cb(spinbox_2,spinbox_cb,LV_EVENT_PRESSED,NULL);

    lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	lv_obj_t* label = lv_label_create(btn1);
	lv_label_set_text(label, "Enter");
	lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, -40, -5);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Cancel");
	lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn2, LV_ALIGN_BOTTOM_MID, 40, -5);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL);  

    keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

    lv_group_add_obj(keyGroup,spinbox_1);
    lv_group_add_obj(keyGroup,spinbox_2);
    lv_group_add_obj(keyGroup,btn1);
    lv_group_add_obj(keyGroup,btn2);    
}
